
import 'package:flutter/material.dart';

class LifeCycleWatcher extends StatefulWidget {

  @override
  _LifeCycleWatcherState createState() => _LifeCycleWatcherState();
}

class _LifeCycleWatcherState extends State<LifeCycleWatcher> with WidgetsBindingObserver{

  AppLifecycleState _lastState;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _lastState = state;
    });
  }

  @override
  Widget build(BuildContext context) {
    if(_lastState == null){
      return new Text('This widget has not observed any lifecycle changes.', textDirection: TextDirection.ltr);
    }
    return new Text('The most recent lifecycle state this widget observed was: $_lastState.',
        textDirection: TextDirection.ltr);
  }
}
